package com.sbs.gaoxinqutoilet.utils;

import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 聪明一只哈 on 2019/6/17.
 * Time: 2019/6/17  10:04
 */
public class NetTimeUtils {

    public static String getNetTime() {

        URL url = null;
        try {
            url = new URL("http://110.249.173.26:8081");
            //url = new URL("http://www.baidu.com");
            //url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
            //url = new URL("http://www.bjtime.cn");
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(ld);
            int i = calendar.get(Calendar.YEAR);
            int j = calendar.get(Calendar.MONTH);
            j = j + 1;
            int x = calendar.get(Calendar.DAY_OF_MONTH);

            String ss=j+"";
            if(ss.length()==1){
                ss="0"+ss;
            }

            String xx=x+"";
            if(xx.length()==1){
                xx="0"+xx;
            }

            String time = i + "-" + ss + "-" + xx;

            return time;
        } catch (Exception e) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(new Date(System.currentTimeMillis()));
            return time;
        }

    }
}
